<?php
class retrieve_newsletter extends retrieve_base{

	private $module_display_options 	= array(
		array("NEWSLETTER_SUBSCRIPTION",	LOCALE_DISPLAY_NEWSLETTER)
	);

		/**
	* function produces abstract structure of modules db structure
	*
    * used to generate the table structure required abstract function that passes an abstract
	* representation of the desired table structure to the proper database module which will
	* interpet the abstract and convert it into a valid SQL Create table structure.
	*
	* @return Array list of abstract table definitions for this module
	*/
	function create_table(){
		$tables = array();
		/**
		* Table structure for table 'contact_data'
		*/
		$fields = array(
			array("newsletter_identifier"	,"unsigned integer"	,"NOT NULL"	,"auto_increment"),
			array("newsletter_client"		,"unsigned integer"	,"NOT NULL"	,"default '0'"),
			array("newsletter_label"		,"varchar(255)"		,"NOT NULL"	,"default ''"),
			array("newsletter_from_label"	,"varchar(255)"		,"NOT NULL"	,"default ''"),
			array("newsletter_from_email"	,"varchar(255)"		,"NOT NULL"	,"default ''"),
			array("newsletter_description"	,"text"				,"NOT NULL"	,"default ''"),
			array("newsletter_date_created"	,"datetime"			,"NOT NULL"	,"default ''"),
			array("newsletter_status"		,"small integer"	,"NOT NULL"	,"default '0'"),
			array("newsletter_location"		,"unsigned integer"	,"NOT NULL"	,"default '0'")
		);
		$primary="newsletter_identifier";
		$tables[count($tables)] = array("newsletter_data", $fields, $primary);
		/**
		* Table structure for table 'newletter_archive' a record of all sent newletters
		*/
		$fields = array(
			array("newsarchive_identifier"	,"unsigned integer"	,"NOT NULL"	,"auto_increment"),
			array("newsarchive_client"		,"unsigned integer"	,"NOT NULL"	,"default '0'"),
			array("newsarchive_subject"		,"varchar(40)"		,"NULL"		,"default ''"),
			array("newsarchive_plaintext"	,"text"				,"NOT NULL"	,""),
			array("newsarchive_rtftext"		,"text"				,"NOT NULL"	,"default ''"),
			array("newsarchive_status"		,"small integer"	,"NOT NULL"	,"default '0'"),
			array("newsarchive_date"		,"datetime"			,"NOT NULL"	,"default ''")
		);
		$primary="newsarchive_identifier";
		$tables[count($tables)] = array("newsletter_archive", $fields, $primary);
		
		/**
		* Table structure for table 'newsletter_subscription' a list of people that have subscribed
		* to a particular mailing list
		*/
		$fields = array(
			array("subscriber_identifier"	,"unsigned integer"	,"NOT NULL"	,"auto_increment"),
			array("subscriber_client"		,"unsigned integer"	,"NOT NULL"	,"default '0'"),
			array("subscriber_email"		,"unsigned integer"	,"NOT NULL"	,"default '0'"),
			array("subscriber_newsletter"	,"unsigned integer"	,"NOT NULL"	,"default '0'"),
			array("subscriber_verified"		,"small integer"	,"NOT NULL"	,"default '0'")
		);
		$primary = "subscriber_identifier";
		$tables[count($tables)] = array("newsletter_subscription", $fields, $primary);

		return $tables;
	}
		function add_to_user_profile($parameters){
		$sign_up 	= $this->check_parameters($parameters,"newsletter_sign_up",Array());
		$user_id	= $this->check_parameters($parameters,"user_identifier",0);
		$max_len 	= count($sign_up);
		if ($user_id>0 && $max_len ==0){
			$sql = "select 
						newsletter_subscription.subscriber_newsletter
					from user_info 
						inner join contact_data on contact_data.contact_user = user_info.user_identifier
						inner join email_addresses on email_addresses.email_contact = contact_data.contact_identifier
						inner join newsletter_subscription on newsletter_subscription.subscriber_email = email_addresses.email_identifier
					where 
						user_identifier=$user_id and user_client=$this->client_identifier";
			$result		= $this->db->query($sql);
			$num_rows 	= $r = $this->db->num_rows($result);;
			if ($num_rows > 0){
				while ($r = $this->db->fetch_array($result)){
					$sign_up[count($sign_up)] = $r["subscriber_newsletter"];
				}
			}
		}
		$max_len 	= count($sign_up);
		$sql		= "select * from newsletter_data where newsletter_client=$this->client_identifier and newsletter_data.newsletter_status = 2"; //live
		$result		= $this->db->query($sql);
		$num_rows 	= $r = $this->db->num_rows($result);;
		if ($num_rows > 0){
			$out = "<input name=\"call_command\" value=\"NEWSLETTER_REGISTER\" type=\"hidden\"/>";
			$out .= "<checkboxes name=\"newsletter_sign_up\" label=\"".LOCALE_SIGNUP_TO_A_NEWSLETTER."\" type='vertical'><options>";
			while ($r = $this->db->fetch_array($result)){
				$out .= "<option value='".$r["newsletter_identifier"]."' ";
				for ($index=0;$index<$max_len;$index++){
					if ($r["newsletter_identifier"] == $sign_up[$index]){
						$out.="selected='true'";
					}
				}
				$out .= "><![CDATA[".$this->check_parameters($r,"newsletter_label")."<br/>".$this->check_parameters($r,"newsletter_description")."]]></option>";
			}
			$out .= "</options></checkboxes>";
		}else{
			$out="";
		}
		return $out;
	}
}
?>